<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION); 
   String token = (String) st.getValue(FormFields.TOKEN);
   boolean parent = (st.getValue("parent") != null);
   String name = (String) st.getValue(SchoolDBSQL.USRFNAME) + " " + (String) st.getValue(SchoolDBSQL.USRLNAME);
   UserContainer teacherContainer = (UserContainer) st.getValue("teacherContainer");
   UserContainer studentContainer = (UserContainer) st.getValue("studentContainer");
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   String indentation = "&nbsp;&nbsp;&nbsp;&nbsp;";
   String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
   String pswdChgReq;
   if (null == teacherContainer) {
    pswdChgReq = studentContainer.getPasswordChangeRequired().toString();
   }
   else {
    pswdChgReq = teacherContainer.getPasswordChangeRequired().toString();
   }
   String displayMessage = (String) st.getValue("displayMessage");
%>


<HTML>
  <HEAD>
    <SCRIPT>
    
    function validateNewPasswords() {
        
        var newPassword1 = document.forms[0].<%= FormFields.NEWPASSWORD %>.value;
        var newPassword2 = document.forms[0].<%= FormFields.RENEWPASSWORD %>.value;
        
        if (newPassword1 == "") {
            alert("You did not specify a password");
            document.forms[0].<%= FormFields.NEWPASSWORD %>.focus();
            return false;
        }
        else if (newPassword1 != newPassword2) {
            alert("The new passwords you entered do not match.\nPlease re-enter your new password in both fields.");
            document.forms[0].<%= FormFields.NEWPASSWORD %>.focus();
            return false;
        }
        else {
            return true;
        }
        
    }
    
    function postProcessing(value) {
        if (value == "Cancel") {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = value;
            document.forms[0].submit();
        }
        else if (validateNewPasswords()) {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = value;
            document.forms[0].submit();
        }
    }
    
    </SCRIPT>
  </HEAD>
  <BODY STYLE="font-family:sans-serif;">
    <H4>Change Password</H4>
    <H5><%= displayMessage %></H5>
    <FORM NAME="changepasswordform" METHOD="POST" onSubmit="return false;">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= token %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PSWDCHGREQ %>" VALUE="<%= pswdChgReq %>">

    <% 
        out.println("<TABLE STYLE=\"font-family:sans-serif;font-size:12;\" WIDTH=\"400\"");
        out.println("  <TR>");
        out.println("    <TD WIDTH=\"200\"><B>Enter current password:&nbsp;&nbsp;</B>" + indentation + "</TD><TD WIDTH=\"200\"><INPUT TYPE=\"PASSWORD\" NAME=\"" + FormFields.CURRENTPASSWORD + "\"></TD>");
        out.println("  </TR>");
        out.println("  <TR>");
        out.println("    <TD WIDTH=\"200\"><B>Enter new password:&nbsp;&nbsp;</B>" + indentation + "</TD><TD WIDTH=\"200\"><INPUT TYPE=\"PASSWORD\" NAME=\"" + FormFields.NEWPASSWORD + "\"></TD>");
        out.println("  </TR>");
        out.println("  <TR>");
        out.println("    <TD WIDTH=\"200\"><B>Re-enter new password:&nbsp;&nbsp;</B>" + indentation + "</TD><TD WIDTH=\"200\"><INPUT TYPE=\"PASSWORD\" NAME=\"" + FormFields.RENEWPASSWORD + "\"></TD>");
        out.println("  </TR>");
        out.println("</TABLE>");
//        out.println("<CENTER><INPUT TYPE=\"SUBMIT\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Change Password\">" + indentation);
        out.println("<BR><INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Change Password\" onClick=\"postProcessing('Change Password')\">" + indentation);
        out.println("<INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Cancel\" onClick=\"postProcessing('Cancel')\">" + indentation);
    %>
    </FORM>
  </BODY>
</HTML>